package com.company.cloud.behavior.modules.jobHandler;

import com.company.cloud.behavior.modules.base.service.*;
import com.company.cloud.behavior.modules.cache.models.BehaviorCountModel;
import com.company.cloud.behavior.modules.cache.service.BehaviorCacheService;
import com.company.cloud.behavior.modules.jobHandler.mapper.BehaviorCountMapper;
import com.company.cloud.common.redis.service.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class BehaviorTaskJob {
    private static Logger logger = LoggerFactory.getLogger(BehaviorTaskJob.class);

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private BehaviorCountMapper behaviorCountMapper;

    @Resource
    private BehaviorCacheService behaviorCacheService;

    @Autowired
    private UserCollectionInfoService userCollectionInfoService;

    @Autowired
    private UserCommentInfoService userCommentInfoService;

    @Autowired
    private UserFollowInfoService userFollowInfoService;

    @Autowired
    private UserThumbsUpInfoService userThumbsUpInfoService;

    @Autowired
    private UserScoreInfoService userScoreInfoService;

    @Scheduled
    public String behaviorHandler(String param) throws Exception {

        // 同步收藏数量
        List<BehaviorCountModel> collectionList = behaviorCountMapper.getCollectionCount();
        collectionList.forEach(item -> {
            behaviorCacheService.resetCollectionCount(item.getObjType(), item.getObjNo(), item.getCount().intValue());
        });

        // 同步评论数量
        List<BehaviorCountModel> commentList = behaviorCountMapper.getCommentCount();
        commentList.forEach(item -> {
            behaviorCacheService.resetCommentCount(item.getObjType(), item.getObjNo(), item.getCount().intValue());
        });

        // 同步评论数量
        List<BehaviorCountModel> followList = behaviorCountMapper.getFollowCount();
        followList.forEach(item -> {
            behaviorCacheService.resetFollowCount(item.getObjType(), item.getObjNo(), item.getCount().intValue());
        });

        // 同步点赞数量
        List<BehaviorCountModel> thumbsUpList = behaviorCountMapper.getThumbsUpCount();
        thumbsUpList.forEach(item -> {
            behaviorCacheService.resetThumbsUpCount(item.getObjType(), item.getObjNo(), item.getCount().intValue());
        });

        // 同步评分数量
        List<BehaviorCountModel> scoreList = behaviorCountMapper.getScoreCount();
        scoreList.forEach(item -> {
            behaviorCacheService.resetScoreCount(item.getObjType(), item.getObjNo(), item.getCount().intValue());
        });

        // 同步评分分数
        List<BehaviorCountModel> scoreAvgList = behaviorCountMapper.getScoreAvg();
        scoreAvgList.forEach(item -> {
            behaviorCacheService.resetScoreAvg(item.getObjType(), item.getObjNo(), item.getCount());
        });

        return "SUCCESS";
    }

    public void init() {
        logger.info("init");
    }

    public void destroy() {
        logger.info("destory");
    }

}
